$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/shoporder/list',
        datatype: "json",
        colModel: [
            { label: '用户', name: 'userId', index: 'USER_ID', width: 70,formatter: function(cellvalue, options, rowdata) {
                    var username="" ;
                    $.ajax({
                        type: "POST",
                        async:false,
                        url: baseURL + "sys/users/info/"+rowdata.userId,
                        success: function(r){
                            username = r.users.username;

                        }
                    });
                    return username;
                }
            },
			{ label: '订单编号', name: 'orderSn', index: 'ORDER_SN', width: 90 },
			{ label: '订单价格', name: 'orderAmount', index: 'ORDER_AMOUNT', width: 40 },
            { label: '订单状态', name: 'orderStatus', width: 50, formatter: function(value, options, row){
                if (value ==0) {
                    return '<span class="label label-danger">租借中</span>' ;
                }  else if (value ==1) {
                    return '<span class="label label-danger">已完成</span>' ;
                }  else if (value ==2) {
                    return '<span class="label label-danger">已撤销</span>' ;
                }
                }},
			{ label: '下单时间', name: 'addTime', index: 'ADD_TIME', width: 80 },
            { label: '二维码编号', name: 'qcode', index: 'qcode', width: 80},
			{ label: '使用时长', name: 'useduration', index: 'useDuration', width: 40 },
			{ label: '截止时间', name: 'endtime', index: 'endtime', width: 80 }, 			
			{ label: '实付金额', name: 'actualAmount', index: 'actual_amount', width: 40 },
            { label: '支付方式', name: 'payType', index: 'pay_type', width: 50, formatter: function(value, options, row){
                    if (value ==0) {
                        return '微信' ;
                    }  else if (value ==1) {
                        return '支付宝' ;
                    }
                } },
            { label: '操作', name: 'sy', width: 100,align:'center',formatter: function(cellvalue, options, rowdata) {
                    /*return "<a style='cursor:pointer;' class='btn btn-primary' onclick='vm.modifyStatus("+ rowdata.id +",0)'>" +
                        "租借中</a>&nbsp;&nbsp;&nbsp;&nbsp;" +
                        "<a style='cursor:pointer;' class='btn btn-primary' onclick='vm.modifyStatus("+ rowdata.id +",1)'>"+
                        "已完成</a>&nbsp;&nbsp;&nbsp;&nbsp;"+
                        "<a style='cursor:pointer;' class='btn btn-primary' onclick='vm.modifyStatus("+ rowdata.id +",2)'>" +
                        "已撤销</a>";*/
                    return "<a style='cursor:pointer;' class='btn btn-primary' onclick='vm.del("+ rowdata.id +")'>" +
                    "删除</a>&nbsp;&nbsp;&nbsp;&nbsp;" +
                    "<a style='cursor:pointer;' class='btn btn-primary' onclick='vm.modifyStatus("+ rowdata.id +",1)'>"+
                        "完成订单</a>";
                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            orderSn: null,
            userId: null,
            merchantid: null,
            status: null
        },
        qcx: {
            name: null,
            district: null,
            status: null
        },
		showList: true,
		title: null,
		shopOrder: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
        queryMerchant: function () {
            vm.reloadMerchant();
        },
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.shopOrder = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.shopOrder.id == null ? "sys/shoporder/save" : "sys/shoporder/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.shopOrder),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
        modifyStatus: function (id,num) {
            // var ids = getSelectedRows();
            // if(ids == null){
            //     return ;
            // }
            var lock = false;
            vm.shopOrder.id = id;
            vm.shopOrder.orderStatus = num;
            layer.confirm('确定要修改选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                if(!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/shoporder/modifyStatus",
                        contentType: "application/json",
                        data: JSON.stringify(vm.shopOrder),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function(){
            });
        },
		del: function (id) {

			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/shoporder/delete/"+id,
                        contentType: "application/json",
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
        addMerchant: function () {
            $("#jqGridAdd").jqGrid({
                url: baseURL + 'sys/merchant/list',
                datatype: "json",
                colModel: [
                    { label: '商户名称', name: 'name', index: 'name', width: 80 },
                    { label: '所属代理商', name: 'agent', index: 'agent', width: 80 },
                    { label: '所属业务员', name: 'businessid', index: 'businessId', width: 80 },
                    { label: '业务员佣金百分比', name: 'businessRatio', index: 'business_RATIO', width: 80 },
                    { label: '省', name: 'province', index: 'PROVINCE', width: 80 },
                    { label: '市', name: 'city', index: 'CITY', width: 80 },
                    { label: '区', name: 'district', index: 'DISTRICT', width: 80 },
                    { label: '详细地址', name: 'address', index: 'ADDRESS', width: 80 },
                    { label: '状态', name: 'status', width: 80, formatter: function(value, options, row){
                            return value === 0 ?
                                '<span class="label label-danger">正常</span>' :
                                '<span class="label label-success">禁止</span>';
                        }},
                    { label: '操作', name: 'sy', width: 70,align:'center',formatter: function(cellvalue, options, rowdata) {
                            return "<a style='cursor:pointer;' onclick='vm.shopChoose("+ rowdata.id +")'>" +
                                "选择</a>&nbsp;&nbsp;&nbsp;&nbsp;" ;
                        }}
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                pager: "#jqGridPager1",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#jqGridAdd").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
            // vm.queryGroup();
            $('#myModal').modal();
        },
        shopChoose:function (id) {
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/merchant/info/"+id,
                success: function(r){
                    $("#merchant").html("");
                    $("#merchant").append(r.merchant.name);
                    vm.merchant.merchantid=r.merchant.id;
                }
            });
            $('#myModal').modal("hide");

        },
		getInfo: function(id){
			$.get(baseURL + "sys/shoporder/info/"+id, function(r){
                vm.shopOrder = r.shopOrder;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
            vm.q.merchantid = vm.shopOrder.merchantid;
			$("#jqGrid").jqGrid('setGridParam',{
                postData: {'status': vm.q.orderStatus,
                    'orderSn': vm.q.orderSn,
                    'userId': vm.q.userId,
                    'merchantid': vm.q.merchantid
                    // 'adminPhone': vm.q.adminPhone
                },
                page:page
            }).trigger("reloadGrid");
		},
        reloadMerchant: function (event) {
            vm.showList = true;
            var page = $("#jqGridAdd").jqGrid('getGridParam','page');
            $("#jqGridAdd").jqGrid('setGridParam',{
                postData: {'status': vm.qcx.status,
                    'name': vm.qcx.name,
                    'district': vm.qcx.district
                },
                page:page
            }).trigger("reloadGrid");
        }
	}
});